/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.controller.sms;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.po.SmsCategoryPo;
import com.itttq.gshop.domain.po.SmsPlatformPo;
import com.itttq.gshop.domain.po.SmsTemplatePo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.connector.ISmsCategoryConnector;
import com.itttq.gshop.generic.connector.ISmsPlatformConnector;
import com.itttq.gshop.generic.connector.ISmsTemplateConnector;

@RestController
@RequestMapping("sms/template")
public class SmsTemplateController extends GenericController {
	private static final long serialVersionUID = 921162097735776765L;
	@Autowired
	private ISmsTemplateConnector smsTemplateConnector;
	@Autowired
	private ISmsPlatformConnector smsPlatformConnector;
	@Autowired
	private ISmsCategoryConnector smsCategoryConnector;
	
	/**
	 * 根据条件查询SmsTemplatePo对象，并分页
	 * 
	 * @param smsTemplate
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("templates.html")
	public ModelAndView getSmsTemplates(SmsTemplatePo smsTemplate, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/sms/templates");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		smsTemplate.setStart(ppt.getStart());
		smsTemplate.setSize(ppt.getSize());
		ResultPageTo<SmsTemplatePo> rpt = smsTemplateConnector.getSmsTemplates(smsTemplate);
		this.getPageMv("templates", mv, rpt);
		mv.addObject("template", smsTemplate);
		return mv;
	}

	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add.html")
	public ModelAndView addSmsTemplate() {
		ModelAndView mv = new ModelAndView("/sms/templateAdd");
		// 获取短信平台和分类
		SmsCategoryPo smsCategory = new SmsCategoryPo();
		smsCategory.setSize(999);
		mv.addObject("categories", smsCategoryConnector.getSmsCategorys(smsCategory));
		SmsPlatformPo smsPlatform = new SmsPlatformPo();
		smsPlatform.setSize(999);
		mv.addObject("platforms", smsPlatformConnector.getSmsPlatforms(smsPlatform));
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}.html")
	public ModelAndView viewSmsTemplate(@PathVariable("id") Integer id) {
		ModelAndView mv = addSmsTemplate();
		if(id != null) {
			mv.addObject("template", smsTemplateConnector.getSmsTemplate(id));
		}
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}.html")
	public ModelAndView editSmsTemplate(@PathVariable("id") Integer id) {
		ModelAndView mv = viewSmsTemplate(id);
		mv.addObject("op", "edit");
		return mv;
	}
	
	/**
	 * 保存SmsTemplatePo对象，无ID则新增，有ID则更新
	 * 
	 * @param smsTemplate
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save.html")
	public ModelAndView saveSmsTemplate(SmsTemplatePo smsTemplate) {
		smsTemplateConnector.saveSmsTemplate(smsTemplate);
		return getSmsTemplates(new SmsTemplatePo(), null);
	}

	/**
	 * 根据ID删除SmsTemplatePo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}.html")
	@ResponseBody
	public ModelAndView delSmsTemplate(@PathVariable("id") Integer id) {
		if(id != null) {
			smsTemplateConnector.delSmsTemplate(id);
		}
		return getSmsTemplates(new SmsTemplatePo(), null);
	}
}